<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>对象排序</title>
  </head>
  <body></body>
  <script>
    var r = [
      { name: "张", age: 23 },
      { name: "恺", age: 16 },
      { name: "新", age: 15 },
      { name: "怜", age: 35 },
      { name: "环", age: 19 },
      { name: "流", age: 17 },
      { name: "延", age: 15 },
      { name: "缠", age: 18 },
    ];
    function objP(r) {
      for (var i = r.length - 1; i >= 0; i--) {
        for (var k = 0; k < i; k++) {
          if (r[k].age > r[k + 1].age) {
            // console.log(r[k].age);
            // var li = {};
            // for (const key in r[k]) {
            //   li[key] = r[k][key];
            // }
            // for (const key in r[k]) {
            //   r[k][key] = r[k + 1][key];
            // }
            // for (const key in li) {
            //   r[k + 1][key] = li[key];
            // }
            var ls = r[k];
            r[k] = r[k + 1];
            r[k + 1] = ls;
          }
        }
      }
    }
    objP(r);
    console.log(r);
  </script>
</html>
